﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace JULONG.WEB.Library
{
    public class AssemblyInfo
    {
        Assembly assembly;

        public AssemblyInfo(Assembly assembly = null)
        {
            if (assembly == null)
                this.assembly = Assembly.GetExecutingAssembly();
            else
            {
                this.assembly = assembly;
            }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public string title
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(assembly.CodeBase);
            }
        }

        /// <summary>
        /// 版本
        /// </summary>
        public string version
        {
            get
            {
                return assembly.GetName().Version.ToString();
            }
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string description
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        /// <summary>
        /// 产品
        /// </summary>
        public string product
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        /// <summary>
        /// 版权
        /// </summary>
        public string copyright
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        /// <summary>
        /// 公司
        /// </summary>
        public string company
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        /// <summary>
        /// GUID
        /// </summary>
        public string GUID
        {
            get
            {
                object[] attributes = assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value;
            }
        }

        public Dictionary<string, string> content()
        {
            Dictionary<string, string> re = new Dictionary<string, string>();
            re.Add("标题", title);
            re.Add("版本", version);
            re.Add("描述", description);
            re.Add("产品", product);
            re.Add("版权", copyright);
            re.Add("公司", company);
            re.Add("GUID", GUID);
            return re;
        }

        public string content(string key)
        {
            if (content().ContainsKey(key))
                return content()[key];
            return null;
        }

    }
}
